
package com.sausageking.client.merchant.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sausageking.shared.YearPassCodes;
import com.sausageking.shared.YearPassCodes.DayPassCode;
import com.sausageking.shared.YearPassCodes.MonthPassCodes;

public class CodeViewImpl extends Composite implements CodeView {

  private static int NUM_MONTH_PER_ROW = 4;

  private static String[] MONTHS = {"January", "February", "March", "April",
      "May", "June", "July", "August", "September", "October", "November",
      "December"};

  private static String[] WEEK_DAYS = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};

  private static CodeViewImplUiBinder uiBinder = GWT
      .create(CodeViewImplUiBinder.class);

  interface CodeViewImplUiBinder extends UiBinder<Widget, CodeViewImpl> {
  }

  @SuppressWarnings("unused")
  private Presenter presenter;

  @UiField
  SimplePanel base;

  private List<FlexTable> monthTables;

  @Override
  public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
  }

  public CodeViewImpl() {
    initWidget(uiBinder.createAndBindUi(this));
    FlexTable yearTable = new FlexTable();
    base.add(yearTable);
    monthTables = new ArrayList<FlexTable>();
    for (int i = 0; i < 12; i++) {
      FlexTable monthTable = new FlexTable();
      monthTables.add(monthTable);
      int row = i / NUM_MONTH_PER_ROW;
      int col = i % NUM_MONTH_PER_ROW;
      yearTable.setWidget(row, col, monthTable);
      yearTable.getCellFormatter().addStyleName(row, col, "month-table");
    }
  }

  @Override
  public void setPassCodeBook(YearPassCodes passCodeBook) {
    for (int i = 0; i < 12; i++) {
      monthTables.get(i).setWidget(0, 0,
          getMonthPassCodeWidget(passCodeBook.getMonthPassCodes().get(i)));
    }
  }

  private Widget getMonthPassCodeWidget(MonthPassCodes monthPassCodes) {
    VerticalPanel base = new VerticalPanel();
    Label monthLabel = new Label(MONTHS[monthPassCodes.getMonthOfYear()]);
    monthLabel.addStyleName("month");
    FlexTable table = new FlexTable();
    // Week day header
    for (int i = 0; i < 7; i++) {
      Label weekLabel = new Label(WEEK_DAYS[i]);
      weekLabel.addStyleName("week");
      table.setWidget(0, i, weekLabel);
    }
    int row = 1;
    for (DayPassCode dayPassCode : monthPassCodes.getDayPassCodes()) {
      int col = dayPassCode.getDayOfWeek() == 7 ? 0 : dayPassCode
          .getDayOfWeek();
      table.setWidget(row, col, getDayPassCodeWidget(dayPassCode));
      if (dayPassCode.getDayOfWeek() == 6) {
        row++;
      }
    }

    base.add(monthLabel);
    base.add(table);
    return base;
  }

  private Widget getDayPassCodeWidget(DayPassCode dayPassCode) {
    Label date = new Label(String.valueOf(dayPassCode.getDayOfMonth()));
    date.addStyleName("date");
    Label code = new Label(String.valueOf(dayPassCode.getPassCode()));
    code.addStyleName("pass-code");
    VerticalPanel panel = new VerticalPanel();
    panel.add(date);
    panel.add(code);
    return panel;
  }
}
